package com.thti.mecool.client.model.biz;

import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.threeti.mecool.mobile.interfaces.dto.ProjectWithStoresAndSellIntoDto;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.Project;
import com.thti.mecool.client.model.po.SalersProject;
import com.thti.mecool.client.model.po.SalersStore;
import com.thti.mecool.client.model.po.Task;

public class SalersStoreBizImpl {
	
	private DataBase dataBase;

	public SalersStoreBizImpl(Context context) {
		dataBase = new DataBase(context);
	}
	public void save(SalersStore salersStore){
		String sqlString="insert into salestore(storeId,projectId,name,provinceAdName,provinceAdCode,cityAdName,cityAdCode,status,tgStockBaseDesc,executionStatus,problemType,problemDescription,description,lastModified) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		dataBase.execObject(sqlString, new String[]{salersStore.getStoreId(),
				salersStore.getProjectId(),salersStore.getName(),salersStore.getProvinceAdName(),
				salersStore.getProvinceAdCode(),salersStore.getCityAdName(),salersStore.getCityAdCode(),
				String.valueOf(salersStore.getStatus()),String.valueOf(salersStore.getTgStockBaseDesc()),
				String.valueOf(salersStore.getExecutionStatus()),String.valueOf(salersStore.getProblemType()),
				salersStore.getProblemDescription(),salersStore.getDescription(),salersStore.getLastModified()});
		dataBase.close();
	}
	
	public void update(SalersStore salersStore){
		String sqlString="update salestore set name=?,provinceAdName=?,provinceAdCode=?,cityAdName=?,cityAdCode=?,status=?,tgStockBaseDesc=?,executionStatus=?,problemType=?,problemDescription=?,description=?,lastModified=? where storeId=? and projectId=?";
		dataBase.execObject(sqlString, new String[]{salersStore.getName(),salersStore.getProvinceAdName(),
				salersStore.getProvinceAdCode(),salersStore.getCityAdName(),salersStore.getCityAdCode(),
				String.valueOf(salersStore.getStatus()),String.valueOf(salersStore.getTgStockBaseDesc()),
				String.valueOf(salersStore.getExecutionStatus()),String.valueOf(salersStore.getProblemType()),
				salersStore.getProblemDescription(),salersStore.getDescription(),salersStore.getLastModified(),salersStore.getStoreId(),
				salersStore.getProjectId()});
		dataBase.close();
	}
	
	/**
	 * 判断storeId是已经存在
	 * @param id
	 * @return
	 */
	public boolean findStoreId(String storeId,String projectId){
		String sqlString="select  * from salestore where storeId=? and projectId=?";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{storeId,projectId});
		
		if(cursor.moveToNext()){
			boolean flag = cursor.getLong(0)>0 ?true:false;
			cursor.close();
			dataBase.close();
			return flag;
		}
		cursor.close();
		dataBase.close();
		return false;

	} 
	
	
	@SuppressWarnings("unchecked")
	public List<SalersStore> findAll(String projectId){
		// select pro.planId,p.planName,p.userId,pro.projectName,t.taskId,t.taskName,t.storeId,t.storeName,t.startTime,t.endTime from plan p,project pro,task t where p.id=pro.planId AND pro.id=t.projectId
		String sql="select  * from  salestore  where projectId=?";
		List<SalersStore> list=dataBase.findAll(SalersStore.class, sql, new String[]{projectId});
		dataBase.close();
		return list;
		
	}

}
